<template>
  <div>
    <el-button type="success" plain @click="handleAddClick" class="addbtn">增加</el-button>
    <el-button type="success" plain @click="commitClick" class="commitbtn">提交</el-button>
<!--    搜索功能，判断data.name是否有search-->
      <el-input
        v-model="search"
        size="mini"
        style="width: 200px;float: right"
        placeholder="输入关键字搜索"/>
<!--表格tableData，过滤search数据-->
    <el-table
      :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID（id）"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名（name）"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sex"
        label="性别（sex）">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄（age）">
      </el-table-column>
      <el-table-column
        prop="class"
        label="班级（class）">
      </el-table-column>



      <el-table-column label="操作" width="180">
<!--        通过solt传递组件-->
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEditClick(scope.$index,scope.row)"  size="mini">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelClick(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改用户" :visible.sync="editBox" width="50%" :before-close="handleClose">
      <el-form ref="form" label-width="100px" v-model="user">
        <el-form-item label="id:">
          <el-input placeholder="请输入id" maxlength="50" v-model = "user.id"></el-input>
        </el-form-item>

        <el-form-item label="名字:">
          <el-input placeholder="请输入名字" maxlength="50" v-model = "user.name"></el-input>
        </el-form-item>

        <el-form-item label="性别:" style="text-align: left">
          <el-radio-group v-model="user.sex" class="sexradio">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="年龄:">
          <el-input placeholder="请输入年龄" maxlength="50" v-model = "user.age"></el-input>
        </el-form-item>

        <el-form-item label="班级:">
          <el-input placeholder="请输入班级" maxlength="50" v-model = "user.class"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleEditUser">确 定</el-button>
            </span>
    </el-dialog>

    <el-dialog title="添加用户" :visible.sync="addBox" width="50%" :before-close="handleClose">
      <el-form ref="form" label-width="100px" v-model="addUserData">
        <el-form-item label="id:">
          <el-input placeholder="请输入id" maxlength="50" v-model = "addUserData.id"></el-input>
        </el-form-item>

        <el-form-item label="名字:">
          <el-input placeholder="请输入名字" maxlength="50" v-model = "addUserData.name"></el-input>
        </el-form-item>

        <el-form-item label="性别:" style="text-align: left">
          <el-radio-group v-model="addUserData.sex" class="sexradio">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="年龄:">
          <el-input placeholder="请输入年龄" maxlength="50" v-model = "addUserData.age"></el-input>
        </el-form-item>

        <el-form-item label="班级:">
          <el-input placeholder="请输入班级" maxlength="50" v-model = "addUserData.class"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleAddUser">确 定</el-button>
            </span>
    </el-dialog>

  </div>
</template>


<script>
  export default {
    name: "TableData",
    data() {
      return {
        tableData: [{
          id: '1',
          name: '小明',
          sex: '男',
          age:'11',
          class:'四年（1）班'
        }, {
          id: '2',
          name: '小红',
          sex: '女',
          age:'12',
          class:'四年（2）班'
        }, {
          id: '3',
          name: '晓东',
          sex: '男',
          age:'11',
          class:'四年（1）班'
        }, {
          id: '4',
          name: '小华',
          sex: '女',
          age:'11',
          class:'四年（2）班'
        }, {
          id: '5',
          name: '小花',
          sex: '女',
          age:'12',
          class:'四年（1）班'
        }],
        addBox : false,
        editBox:false,
        user:{},
        editIndex:"",
        addUserData:{
          id : "",
          name : "",
          sex : "",
          age:'',
          class:''
        },
        search: ''
      }
    },
    methods:{
      // user值不实时改变，而是确认后赋值
      handleEditClick(index,row){
        this.editBox = true;
        this.user = {...row};
        this.editIndex = index;
      },

      // eslint-disable-next-line no-unused-vars
      handleDelClick(index,row){
        this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(index,1)
          this.$message({showClose: true, message: "删除成功", type: 'success'});
        })
      },
      handleClose(done) {
        done();
        this.addUserData = {
          id : "",
          name : "",
          sex : "",
          age:'',
          class:''};
        // this.user = {};
      },
      handleEditUser(){
        //不用require判断，改用message提示 3）	模拟异常提示
        if(this.user.id==""){
          this.$message({showClose: true, message: "id为空！", type: 'false'});
          return;
        }
        if(this.user.name==""){
          this.$message({showClose: true, message: "姓名为空！", type: 'false'});
          return;
        }

        if(this.user.age==""){
          this.$message({showClose: true, message: "年龄为空！", type: 'false'});
          return;
        }
        if(this.user.class==""){
          this.$message({showClose: true, message: "班级为空！", type: 'false'});
          return;
        }
        this.tableData.splice(this.editIndex,1,this.user)
        this.$message({showClose: true, message: "修改成功", type: 'success'});
        this.editBox = false
      },
      handleAddClick(){
        this.addBox = true
      },
      commitClick(){
        //模拟提交
        this.$message({showClose: true, message: "模拟提交成功", type: 'success'});
      },
      handleAddUser(){
        // let strDate = dateFormat("YYYY-mm-dd",this.addUserData.date)
        // this.addUserData.date = strDate
        if(this.addUserData.id==""){
          this.$message({showClose: true, message: "id为空！", type: 'false'});
          return;
        }
        if(this.addUserData.name==""){
          this.$message({showClose: true, message: "姓名为空！", type: 'false'});
          return;
        }
        if(this.addUserData.sex==""){
          this.$message({showClose: true, message: "性别为空！", type: 'false'});
          return;
        }
        if(this.addUserData.age==""){
          this.$message({showClose: true, message: "年龄为空！", type: 'false'});
          return;
        }
        if(this.addUserData.class==""){
          this.$message({showClose: true, message: "班级为空！", type: 'false'});
          return;
        }
        this.tableData.push(this.addUserData)
        this.addBox = false
        this.addUserData = {}
        this.$message({showClose: true, message: "添加成功", type: 'success'});
      }
    }
  }


</script>

<style scoped>
.addbtn{
  float: left;
}
.commitbtn{
  float: right;
}

</style>
